package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.teaminfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.utils.BootstrapUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.TeamInfoMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.TeamInfo;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.employee.JsmEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.file.JsmFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.teaminfo.JsmTeamInfoService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.teaminfo.JsmTeamMembersService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zmling on 2016/12/14.
 * 班组信息
 */

public class JsmTeamInfoServiceImpl implements JsmTeamInfoService {

    @Autowired
    private TeamInfoMapper teamInfoMapper;

    @Autowired
    private JsmEmployeeService employeeService;
    @Autowired
    private JsmFileService fileService;
    @Autowired
    private JsmTeamMembersService jsmTeamMembersService;

    /**
     * 新增
     *
     * @param teamInfo
     * @param uploadFiles
     * @param deletedFiles
     * @return
     * @throws Exception
     */
    @Override
    public int teamInfoAdd(TeamInfo teamInfo, String uploadFiles, String deletedFiles, Employee employee) throws Exception {
        teamInfo.setTeamId(UniqueUtil.uuid());
        teamInfo.setCreatorId(employee.getEmployeeId());
        teamInfo.setCreateDate(new Date());
        teamInfo.setModifierId(employee.getEmployeeId());
        teamInfo.setModifyDate(new Date());
        teamInfo.setStatus((short) 0);
        teamInfoMapper.insertSelective(teamInfo);
        int b = fileService.addFile(uploadFiles, deletedFiles, teamInfo.getTeamId());
        return b;
    }

    /**
     * 分页查询
     *
     * @param bootstrapSearchBean
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject findTeamInfoId(BootstrapSearchBean bootstrapSearchBean) throws Exception {
        JSONObject jsonObject = new JSONObject();
        bootstrapSearchBean.setLimit(bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit());
        bootstrapSearchBean.setOffset(bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset());
        bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
        if (bootstrapSearchBean.getSearch() != null && bootstrapSearchBean.getSearch() != "") {
            bootstrapSearchBean.setSearch(bootstrapSearchBean.getSearch().trim());
        }
        jsonObject.put("total", teamInfoMapper.queryCount(bootstrapSearchBean));
        jsonObject.put("rows", JSON.toJSON(teamInfoMapper.queryList(bootstrapSearchBean)));
        return jsonObject;
    }

    /**
     * 通过Id查询
     *
     * @param teamId
     * @return
     * @throws Exception
     */
    @Override
    public TeamInfo findTeamInfo(String teamId) throws Exception {
        return teamInfoMapper.selectByPrimaryKey(teamId);
    }

    /**
     * 修改
     *
     * @param teamInfo
     * @param uploadFiles
     * @param deletedFiles
     * @return
     * @throws Exception
     */
    @Override
    public int teamInfoUpdate(TeamInfo teamInfo, String uploadFiles, String deletedFiles, Employee employee) throws Exception {
        teamInfo.setModifierId(employee.getEmployeeId());
        teamInfo.setModifyDate(new Date());
        teamInfoMapper.updateByPrimaryKeySelective(teamInfo);
        int a = fileService.updateFile(uploadFiles, deletedFiles, teamInfo.getTeamId());
        return a;
    }

    /**
     * 删除
     *
     * @param teamIds
     * @return
     * @throws Exception
     */
    @Override
    public int deleteTeam(Map teamIds) throws Exception {
        //先删除班组下的成员
        jsmTeamMembersService.updateTeamId(teamIds);
        return teamInfoMapper.updateTeamByIds(teamIds);
    }

    /**
     * 查询
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<TeamInfo> findTeamInfos(TeamInfo record) throws Exception {
        if(StringUtils.isNotEmpty(record.getTeamId()) && StringUtils.isNotEmpty(record.getOrgId())){
            List<TeamInfo> list = teamInfoMapper.selectTeamInfos(record);
            return list;
        }
        return null;
    }

    /**
     * 查询班组信息
     *
     * @return
     */
    @Override
    public List<TeamInfo> selectTeamInfos() {
        return teamInfoMapper.selectTeamInfos(null);
    }

    /**
     * 根据人员信息ID查询班组
     *
     * @param employeeId
     * @return
     */
    @Override
    public TeamInfo selectByParam(String employeeId) {
        return teamInfoMapper.selectByParam(employeeId);
    }
}
